package com.imcparts;

/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.namespace.QName;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.ws.security.wss4j.WSS4JOutInterceptor;
import org.apache.ws.security.WSConstants;
import org.apache.ws.security.handler.WSHandlerConstants;

import com.busdepot.imc.ClientPasswordHandler;
import com.busdepot.webapp.domain.AbstractPart;
import com.imcparts.schema.CategoryInformationType;
import com.imcparts.schema.LimitedProductInformationType;
import com.imcparts.schema.LineInformationType;
import com.imcparts.schema.OrderItemDetailType;
import com.imcparts.schema.OrderTrackingType;
import com.imcparts.schema.OrderType;
import com.imcparts.schema.ProductInformationType;
import com.imcparts.schema.VehicleInformationType;

/**
 * This class was generated by Apache CXF 2.2.5 Fri Dec 11 12:53:01 EST 2009
 * Generated source version: 2.2.5
 * 
 */

public final class ImcTestClient {

	private static final QName SERVICE_NAME = new QName(
			"http://www.imcparts.com", "IMC-CatalogLeasingServices");

	private ImcTestClient() {
	}

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String args[]) throws Exception {
		URL wsdlURL = Thread.currentThread().getClass().getResource("/imc.wsdl"); 
		IMCCatalogLeasingServices ss = new IMCCatalogLeasingServices(wsdlURL,
				SERVICE_NAME);
		IMCCatalogLeasingServicesPortType port = ss.getCatalogServices();

		Client client = configureAuthentication(port);

		String accountNumber = "444449";
		String apiVersionId = "20060912";
		String orderID = new String("290739");

		{
			
			
			
			
			// NOTES: if you enter a bad make, nothing is returned \ empty list
			// if you enter a bad year, nothing is returned \ empty list

			{
				System.out.println("Invoking getMakeInformation...");
				List<String> makeList = port.getMakeInformation(apiVersionId,
						accountNumber);
				System.out.println("getMakeInformation.result: List size = "
						+ makeList.size());
				for (String make : makeList) {
					System.out.println(make);
				}
			}

			/*

			System.out.println("Invoking getVehicleInformation...");
			String vehicleMake = "Honda";
			String vehicleYear = "2000";
			List<com.imcparts.schema.VehicleInformationType> vehicleList = port
					.getVehicleInformation(apiVersionId, accountNumber,
							vehicleMake, vehicleYear);
			System.out.println("");
			System.out
					.println("getVehicleInformation.result  ---------------- "
							+ vehicleList.size() + " vehicles matched: ");
			System.out.println("");
			for (VehicleInformationType vehicle : vehicleList) {
				System.out.println("Make: " + vehicle.getMake());
				System.out.println("Model: " + vehicle.getModel());
				System.out.println("Year: " + vehicle.getYear());
				System.out.println("Submodel: " + vehicle.getSubmodel());
				System.out.println("VID: " + vehicle.getVID());
				System.out.println("Aspiration: " + vehicle.getAspiration());
				System.out.println("CC: " + vehicle.getCC());
				System.out.println("CID: " + vehicle.getCID());
				System.out.println("Engine Designation: "
						+ vehicle.getEngineDesignation());
				System.out.println("Engine Type: " + vehicle.getEngineType());
				System.out.println("Fuel Type: " + vehicle.getFuel());
				System.out.println("Fuel Delivery: "
						+ vehicle.getFuelDelivery());
				System.out.println("Liter capacity: " + vehicle.getLiter());
				System.out.println("");
			}
		}


		{
			// Notes: if you pass in a non-matching VID, nothing is returned \
			// empty list
			// if you pass multiple VIDs only matches are returned

			System.out.println("Invoking getCategoryInformation...");
			System.out.println("");
			List<String> vidList = new ArrayList();
			//vidList.add("1433019");
			vidList.add("1371428");
			List<com.imcparts.schema.CategoryInformationType> categoryList = port
					.getCategoryInformation(apiVersionId, accountNumber,
							vidList);
			System.out.println("getCategoryInformation.result: List size =  "
					+ categoryList.size());
			for (CategoryInformationType category : categoryList) {
				System.out.println(category.getCategoryName());
			}
		}

		
		{
			// NOTES: If you pass in a single VID that does not match, nothing
			// is returned \ empty list
			// If you pass multiple VID's, only matching results are returned
			// If you pass a bad category, nothing is returned \ empty list
			// If you pass multiple categories, only matching results are
			// returned, non-matches ignored

			System.out.println("Invoking getLineInformation...");
			List<String> vidList = new ArrayList();
			// vidList.add("1431687");
			vidList.add("1371428");
			List<String> categoryList = new ArrayList();
			categoryList.add("Brake Components & Wheels");
			//categoryList.add("Filters");
			List<com.imcparts.schema.LineInformationType> lineList = port
					.getLineInformation(apiVersionId, accountNumber, vidList,
							categoryList);
			System.out.println("getLineInformation.result: List size = "
					+ lineList.size());
			for (LineInformationType line : lineList) {
				System.out.println("Class: " + line.getClassName());
				System.out.println("    Line: " + line.getLineName());
			}
		}

		
		{
			// NOTES:
			// If you pass one line & VID and it does not match the system
			// returns empty list
			// If you enter some good and some bad data, system returns any
			// matching data (ignores bad)
			// pricing information does not seem correct. Wholesale prices seems
			// VERY high and I rarely find a suggested retail price

			System.out.println("Invoking getProductInformation...");
			List<String> vidList = new ArrayList();
			// vidList.add("1091966");
			vidList.add("1371428");
			List<String> lineList = new ArrayList();
			lineList.add("Front Brake Disc");
			//lineList.add("Fresh Air Filter");
			List<com.imcparts.schema.ProductInformationType> productInfo = port
					.getProductInformation(apiVersionId, accountNumber,
							vidList, lineList);
			System.out.println("getProductInformation.result: List size = "
					+ productInfo.size());
			for (ProductInformationType product : productInfo) {
				System.out.println("Part Name: " + product.getPartName());
				System.out.println("Part Number: " + product.getPartNumber());
				System.out.println("Description: "
						+ product.getPartDescription());
				System.out.println("Supplier Price: " + product.getPrice());
				System.out.println("Suggested retail price: "
						+ product.getListPrice());
				System.out.println("Brand: " + product.getBrand());
				System.out.println("Core charge: " + product.getCoreCharge());
				System.out.println("IMC Part Number: "
						+ product.getIMCPartNumber());
				System.out.println("Warehouse Inventory #1: "
						+ product.getInventory01());
				System.out.println("Warehouse Inventory #2: "
						+ product.getInventory07());
				System.out.println("Nonfitment notes: "
						+ product.getNonfitmentNotes());
				System.out.println("Oversize notes: " + product.getOversize());
				System.out.println("Pack size: " + product.getPackSize());
				System.out.println("Quantity Required: "
						+ product.getQtyRequired());
				System.out.println("Remark #1: " + product.getRemark1());
				System.out.println("Remark #2: " + product.getRemark2());
				System.out.println("Weight: " + product.getWeight());
				System.out.println("");

			}
		}

	
		{
			System.out
					.println("Invoking getUniversalAccessoriesLineInformation...");
			List<com.imcparts.schema.LineInformationType> universalAccessoriesList = port
					.getUniversalAccessoriesLineInformation(apiVersionId,
							accountNumber);
			System.out.println("getUniversalAccessoriesLineInformation.result="
					+ universalAccessoriesList);
			System.out.println("");
			System.out.println("List size: " + universalAccessoriesList.size());
			System.out.println("");
			for (LineInformationType line : universalAccessoriesList) {
				System.out.println("Class: " + line.getClassName());
				System.out.println("      Line: " + line.getLineName());
				System.out.println("");
			}
		}

		{
			// NOTES: non matching part number returns nothing \ empty list
			// part number 11 42 7 541 827 MN returned two results (w and w\o
			// MN)
			// part number 11 42 7 541 827 returned two results (w and w\o MN)
			// in combination search, non matching part numbers are ignored and
			// matches are returned

			System.out.println("Invoking partNumberSearch...");
			List<String> IMCpartNumber = new ArrayList();
			// IMCpartNumber.add("HON073112");
			// IMCpartNumber.add("11 42 7 541 827 MN");
			// IMCpartNumber.add("11 42 7 541 827");
			// IMCpartNumber.add("VWW032262");
			List<com.imcparts.schema.LimitedProductInformationType> partList = port
					.partNumberSearch(apiVersionId, accountNumber,
							IMCpartNumber);
			System.out.println("partNumberSearch.result=" + partList);
			for (LimitedProductInformationType productInfo : partList) {
				System.out.println("Part name:  " + productInfo.getPartName());
				System.out.println("Part number: "
						+ productInfo.getPartNumber());
				System.out.println("Part Description: "
						+ productInfo.getPartDescription());
				System.out.println("Brand Name: " + productInfo.getBrand());
				System.out.println("Core charge: "
						+ productInfo.getCoreCharge());
				System.out.println("IMC Part Number: "
						+ productInfo.getIMCPartNumber());
				System.out.println("Warehouse #1 Inventory: "
						+ productInfo.getInventory01());
				System.out.println("Warehouse #2 Inventory: "
						+ productInfo.getInventory07());
				System.out.println("List price: " + productInfo.getListPrice());
				System.out.println("Nonfitment notes: "
						+ productInfo.getNonfitmentNotes());
				System.out.println("Oversize notes: "
						+ productInfo.getOversize());
				System.out.println("Pack size: " + productInfo.getPackSize());
				System.out.println("Price: " + productInfo.getPrice());
				System.out.println("Quantity required: "
						+ productInfo.getQtyRequired());
				System.out.println("Weight: " + productInfo.getWeight());
				System.out.println("");
			}
		}

		{
			// NOTES: if you pass bad line information, system throws ugly
			// exception

			System.out
					.println("Invoking getUniversalAccessoriesProductInformation...");
			String line = "Electric Fuel Pump";
			// String line = "boogie";
			List<com.imcparts.schema.LimitedProductInformationType> universalAccessoriesList = port
					.getUniversalAccessoriesProductInformation(apiVersionId,
							accountNumber, line);
			System.out
					.println("getUniversalAccessoriesProductInformation.result = List Size: "
							+ universalAccessoriesList.size());
			System.out.println("");
			for (LimitedProductInformationType productInfo : universalAccessoriesList) {
				System.out.println("Part name:  " + productInfo.getPartName());
				System.out.println("Part number: "
						+ productInfo.getPartNumber());
				System.out.println("Part Description: "
						+ productInfo.getPartDescription());
				System.out.println("Brand Name: " + productInfo.getBrand());
				System.out.println("Core charge: "
						+ productInfo.getCoreCharge());
				System.out.println("IMC Part Number: "
						+ productInfo.getIMCPartNumber());
				System.out.println("Warehouse #1 Inventory: "
						+ productInfo.getInventory01());
				System.out.println("Warehouse #2 Inventory: "
						+ productInfo.getInventory07());
				System.out.println("List price: " + productInfo.getListPrice());
				System.out.println("Nonfitment notes: "
						+ productInfo.getNonfitmentNotes());
				System.out.println("Oversize notes: "
						+ productInfo.getOversize());
				System.out.println("Pack size: " + productInfo.getPackSize());
				System.out.println("Price: " + productInfo.getPrice());
				System.out.println("Quantity required: "
						+ productInfo.getQtyRequired());
				System.out.println("Weight: " + productInfo.getWeight());
				System.out.println("");
			}
		}

		{
			// NOTES: If you pass in a keyword that does not match, the list is
			// returned and size is 0
			// if you pass in a matching keyword with bad VID, the list is
			// returned and size is 0
			// if you pass in a matching keyword with good and bad VID, only
			// matches are returned

			System.out.println("Invoking keywordSearch...");
			String keyword = "pump";
			// String keyword = "boogie";
			List<String> vidList = new ArrayList();
			vidList.add("1433812");
			// vidList.add("342");
			List<com.imcparts.schema.LineInformationType> keywordSearchList = port
					.keywordSearch(apiVersionId, accountNumber, keyword,
							vidList);
			System.out.println("keywordSearch.result ---- List size: "
					+ keywordSearchList.size());
			for (LineInformationType line : keywordSearchList) {
				System.out.println("Line: " + line.getLineName());
			}
		}

		{
			// NOTES: poNumber needs to be unique or you get a nasty exception
			// bombs if you pass in a bad part number
			// bombs if you order more than they have on inventory!!! API states
			// that this only occurs if
			// you specify a specific warehouse 1 or 7
			// bombs if you don't specify a quantity for an item
			// bombs if you don't specify a comment for an item
			// allows you to submit letters in the quantity section

			System.out.println("Invoking submitOrder...");
			String orderComment = "Test order";
			String shipVia = "NXTDAYSAVR";
			// UPS GROUND, NXTDAYSAVR, 2ND DAYAIR, FEDEX GRND, FED PRIOR
			// FEDEX 1DAY, FEDEX 2DAY, FEDEX 3DAY
			String warehouse = "0";
			// 0, 1, 7
			Double x = Math.random();
			int random = (int) (1000000 * x);
			String poNumber = ("W" + random);
			String dropshipName = "John Smith";
			String dropshipAddress1 = "100 Main Street";
			String dropshipAddress2 = "Apt 5";
			String dropshipAddress3 = "";
			String dropshipCity = "Danville";
			String dropshipState = "PA";
			String dropshipZip = "17821";
			List<com.imcparts.schema.OrderItemDetailType> itemList = new ArrayList();

			OrderItemDetailType orderItem = new OrderItemDetailType();
			orderItem.setPartNumber("HON073112");
			orderItem.setQuantity("1");
			orderItem.setComment("This is just a test");
			itemList.add(orderItem);
			OrderItemDetailType orderItem2 = new OrderItemDetailType();
			orderItem2.setPartNumber("11 42 7 541 827");
			orderItem2.setQuantity("3");
			orderItem2.setComment("Just a test");
			itemList.add(orderItem2);
			OrderItemDetailType orderItem3 = new OrderItemDetailType();
			orderItem3.setPartNumber("BMW088480");
			orderItem3.setQuantity("1");
			orderItem3.setComment("");
			itemList.add(orderItem3);
			OrderItemDetailType orderItem4 = new OrderItemDetailType();
			orderItem4.setPartNumber("BMW000476");
			orderItem4.setComment("");
			orderItem4.setQuantity("2");

			System.out.println(poNumber);
			List<com.imcparts.schema.OrderType> submitOrderList = port
					.submitOrder(apiVersionId, accountNumber, orderComment,
							shipVia, warehouse, poNumber, dropshipName,
							dropshipAddress1, dropshipAddress2,
							dropshipAddress3, dropshipCity, dropshipState,
							dropshipZip, itemList);
			System.out.println("submitOrder.result=" + submitOrderList);

			for (OrderType orderItemDetail : submitOrderList) {
				System.out.println("Dropship fee: "
						+ orderItemDetail.getDropshipFee());
				System.out.println("Freight charge: "
						+ orderItemDetail.getFreightCharge());
				System.out.println("Merchandise total: "
						+ orderItemDetail.getMerchandiseTotal());
				System.out.println("Message: " + orderItemDetail.getMessage());
				System.out.println("Order ID: " + orderItemDetail.getOrderId());
				orderID = orderItemDetail.getOrderId();
				System.out.println("Tax: " + orderItemDetail.getTax());
				System.out.println("WareHouse: "
						+ orderItemDetail.getWarehouse());

			}
		}

		{ // NOTES: appears to timeout if you pass it a bad orderID

			System.out.println("Invoking getOrderTracking...");
			List<String> orderTrackingId = new ArrayList();
			orderTrackingId.add(orderID);
			List<com.imcparts.schema.OrderTrackingType> orderTrackingList = port
					.getOrderTracking(apiVersionId, accountNumber,
							orderTrackingId);
			System.out.println("getOrderTracking.result=" + orderTrackingList);
			for (OrderTrackingType orderTracking : orderTrackingList) {
				System.out
						.println("Shipped Via: " + orderTracking.getShipVia());
				System.out
						.println("Status: "
								+ orderTracking.getStatus()
								+ "   (Codes: 100 = invalid order *** 200 = not shipped yet *** 300 = shipped)");
				System.out.println("Tracking Number: "
						+ orderTracking.getTrackingNumber());
			}
			
			*/
			
		}

	}

	public static Client configureAuthentication(
			IMCCatalogLeasingServicesPortType port) {
		// configure authentication
		Client client = org.apache.cxf.frontend.ClientProxy.getClient(port);
		Endpoint cxfEndpoint = client.getEndpoint();

		Map<String, Object> inProps = new HashMap<String, Object>();
		// how to configure the properties is outlined below;

		// WSS4JInInterceptor wssIn = new WSS4JInInterceptor(inProps);
		// cxfEndpoint.getInInterceptors().add(wssIn);

		Map<String, Object> outProps = new HashMap<String, Object>();
		// how to configure the properties is outlined below;

		outProps.put(WSHandlerConstants.ACTION,
				WSHandlerConstants.USERNAME_TOKEN);
		// Specify our username
		outProps.put(WSHandlerConstants.USER, "busdepottest");
		// Password type : plain text
		outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
		outProps.put(WSHandlerConstants.MUST_UNDERSTAND, "false");

		// for hashed password use:
		// outProps.put(WSHandlerConstants.PASSWORD_TYPE,
		// WSConstants.PW_DIGEST);
		// Callback used to retrieve password for given user.
		outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,
				ClientPasswordHandler.class.getName());

		WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
		cxfEndpoint.getOutInterceptors().add(wssOut);
		return client;
	}

}
